package com.api.oa.mapper;

import com.api.oa.model.entity.YQFaceAttend;
import com.api.oa.model.vo.OaNoticeVo;
import com.api.oa.model.vo.YQFaceAttendVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface YQFaceAttendMapper {


    @Insert("insert into  yq_attend  (" +
            "userId," +
            "lat," +
            "lng," +
            "status," +
            "type," +
            "patientUserId," +
            "orderCode," +
            "insCode," +
            "createdAt" +
            ") " +
            "values " +
            "(" +
            "#{entity.userId}," +
            "#{entity.lat}," +
            "#{entity.lng}," +
            "#{entity.status}," +
            "#{entity.type}," +
            "#{entity.patientUserId}," +
            "#{entity.orderCode}," +
            "#{entity.insCode}," +
            "#{entity.createdAt}" +
            ")" )
    @Options(useGeneratedKeys = true, keyProperty = "attendId", keyColumn = "id")
    Integer insert(@Param("entity") YQFaceAttend entity);


    @Update("update  yq_attend  set " +
        "userId = #{entity.userId}," +
        "lat = #{entity.lat}," +
        "lng = #{entity.lng}," +
        "status = #{entity.status}," +
        "type = #{entity.type}," +
        "patientUserId = #{entity.patientUserId}," +
        "orderCode = #{entity.orderCode}," +
        "insCode = #{entity.insCode}," +
        "createdAt = #{entity.createdAt}" +
    " where attendId = #{entity.attendId}")
    Integer update(@Param("entity") YQFaceAttend  entity);


    @Delete("delete from yq_attend where attendId=#{id}")
    void delete(@Param("id") Long id);


    @Select("<script> select yq_attend.*,yq_user.uname  from  yq_attend left join yq_user on yq_attend.userId = yq_user.userId  <where>  1 = 1" +
            " <if test='insCode!=null and insCode!=\"\"'> and yq_user.belongInsCode =#{insCode} </if> " +
            "<if test='status!=null and status!=\"\"'> and yq_attend.status =#{status} </if> " +
            "<if test='type!=null and type!=\"\"'> and yq_attend.type =#{type} </if> " +
            "<if test='role!=null and role!=\"\"'> and yq_user.userType =#{role} </if></where>  </script>")
    List<YQFaceAttendVO> list(@Param("insCode") String insCode, @Param("status") String status,@Param("role") String role, @Param("type") String type);


    @Select("select * from  yq_attend  where attendId = #{id} limit 1")
    YQFaceAttend findOne(@Param("id") Long id);


    /**
     * 每个用户的打开记录
     * @param userId
     * @return
     */
    @Select("select distinct(date_format(createdAt,'%Y-%m-%d')) from    yq_attend  where type = 0  and userId = #{userId} and YEARWEEK( date_format(createdAt,'%Y-%m-%d')) = YEARWEEK(now())")
    List<String> findWeekAttend(@Param("userId") Long userId);

}

